//
//  LPMessageListViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/24.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPMessageListViewController.h"
#import "JPUSHService.h"
#import "ZJPushTableViewCell.h"
#import "MyBranchThreeTableViewCell.h"
#import "ZJPushInfoManager.h"
#import "ZJShareClaimViewController.h"
#import "GTResouseDetailViewController.h"
#import "ShareResouceDetailViewController.h"
#import "LPActivityDetailViewController.h"
#import "OnlineOLDetailViewController.h"
#import "OnlineOrganizationalLifeModel.h"
#import "ZJWebViewController.h"
#define CELLID @"ZJPushTableViewCell"

@interface LPMessageListViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;

@end

@implementation LPMessageListViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSMutableArray *array = [[ZJPushInfoManager sharedPushInfo] readPushInfo];
    self.dataSouce = [[NSArray yy_modelArrayWithClass:[ZJPushInfoModel class] json:array] mutableCopy];
    if (self.dataSouce.count == 0) {
        [LYEmptyManager showNoDataWithView:self.tableView];
    }
    [self.tableView reloadData];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"消息";
    [self layoutUI];
    
    NSMutableArray *array = [[ZJPushInfoManager sharedPushInfo] readPushInfo];
    self.dataSouce = [[NSArray yy_modelArrayWithClass:[ZJPushInfoModel class] json:array] mutableCopy];
    if (self.dataSouce.count == 0) {
       
        [LYEmptyManager showNoDataWithView:self.tableView];
        
    }
    [self.tableView reloadData];
    if (self.dataSouce.count > 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataSouce.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}
#pragma mark - 数据
#pragma mark - 界面
- (void)layoutUI{
    
    [self.view addSubview:self.tableView];
}

/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJPushInfoModel *model = self.dataSouce[indexPath.row];
    return model.cellHeight;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJPushTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJPushInfoModel *model  =  self.dataSouce[indexPath.row];
//   判断消息是否可以点击进入
//    if (model.canRead.length > 0) {
//        if ([model.canRead integerValue] == 0) {
//            return;
//        }
//    }
    //改变阅读状态
    
    if ([model.state integerValue] != 1) {
        model.state = @"1";
        [[ZJPushInfoManager sharedPushInfo] addPushMessage:[model yy_modelToJSONObject]];
        NSInteger unReadNumber = [[ZJPushInfoManager sharedPushInfo] unReadMessageNumber];
        [UIApplication sharedApplication].applicationIconBadgeNumber = unReadNumber;
        [JPUSHService setBadge:unReadNumber];
    }
    /**  0、链接 1、学习  2、活动  3、直播 4、新闻 5、需求  6、资源  7、审核消息  8、请假消息  9、催缴党费 */
    NSString *ID = @"";
    if ([model.dataDict.allKeys containsObject:@"id"]) {
        ID = [model.dataDict objectForKey:@"id"];
        
    }
    else{
        ID = [model.dataDict objectForKey:@"ID"];
    }
    if ([model.type isEqualToString:@"0"] && ID) {//链接
        ZJWebViewController *vc = [ZJWebViewController new];
        vc.urlString = [model.dataDict objectForKey:@"url"];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.type isEqualToString:@"2"] && ID) {//活动
        NSString *big_class = [model.dataDict objectForKey:@"BIG_CLASS"];
        NSString *ONLINE = [model.dataDict objectForKey:@"ONLINE"];
        if ([big_class isEqualToString:@"1"] && [ONLINE isEqualToString:@"1"]) {
            OnlineOLDetailViewController *vc = [OnlineOLDetailViewController new];
            OnlineOrganizationalLifeModel *online = [OnlineOrganizationalLifeModel new];
            online.ID = ID;
            online.NAME = model.title;
            vc.model = online;
            vc.block = ^{
                 [[ZJPushInfoManager sharedPushInfo] removePushMessageWithID:model.ID];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            LPActivityDetailViewController *vc = [LPActivityDetailViewController new];
            ActivityJoinDetailModel *model1= [ActivityJoinDetailModel new];
            model1.ID = ID;
            model1.NAME = model.title;
            vc.model = model1;
            vc.actBlock = ^() {
                [[ZJPushInfoManager sharedPushInfo] removePushMessageWithID:model.ID];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }

    }else if ([model.type isEqualToString:@"5"] && ID) {//需求
        ZJShareClaimViewController *vc = [ZJShareClaimViewController new];
        ShareOpenRequestModel *model1 = [ShareOpenRequestModel new];
        vc.pushID = model.ID;
        vc.noDataBlock = ^(NSString *PUSHID) {
           [[ZJPushInfoManager sharedPushInfo] removePushMessageWithID:PUSHID];
        };
        model1.ID = ID;
        vc.model = model1;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.type isEqualToString:@"6"] && ID) {//资源
        /**  资源类型判断是预约资源还是发布资源  */
        NSString *type = [model.dataDict objectForKey:@"type"];
        /**  type:1资源,2预约  */
        if ([type isEqualToString:@"1"]) {
            ShareResouceDetailViewController *vc = [ShareResouceDetailViewController new];
            vc.ID = ID;
            vc.type = [model.dataDict objectForKey:@"r_type"];
            vc.block  = ^() {
                [[ZJPushInfoManager sharedPushInfo] removePushMessageWithID:model.ID];
            };
            
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
            GTResouseDetailViewController *vc = [GTResouseDetailViewController new];
            vc.ID = ID;
            
            vc.block  = ^() {
                [[ZJPushInfoManager sharedPushInfo] removePushMessageWithID:model.ID];
            };
            
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[ZJPushTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



@end
